//
//  CAGradientLayer+Extension.swift
//  MT
//
//  Created by zhang qiang on 2018/11/30.
//  Copyright © 2019年 张强. All rights reserved.
//

import Foundation
import UIKit


extension CAGradientLayer {
    //获取彩虹渐变层
    public func rainbowLayer() -> CAGradientLayer {
        //定义渐变的颜色（7种彩虹色）
        let gradientColors = [UIColor.red.cgColor,
                              UIColor.orange.cgColor,
                              UIColor.yellow.cgColor,
                              UIColor.green.cgColor,
                              UIColor.cyan.cgColor,
                              UIColor.blue.cgColor,
                              UIColor.purple.cgColor]
        
        //定义每种颜色所在的位置
        let gradientLocations:[NSNumber] = [0.0, 0.17, 0.33, 0.5, 0.67, 0.83, 1.0]
        
        //创建CAGradientLayer对象并设置参数
        self.colors = gradientColors
        self.locations = gradientLocations
        
        //设置渲染的起始结束位置（横向渐变）
        self.startPoint = CGPoint(x: 0, y: 0)
        self.endPoint = CGPoint(x: 1, y: 0)
        
        return self
    }
    
    public func caGradientLayer(gradientColors: [CGColor]?, gradientLocations:[NSNumber]?, startPoint: CGPoint?, endPoint: CGPoint?) -> CAGradientLayer {
        //定义渐变的颜色
        let gradientColors = gradientColors ?? [UIColor.red.cgColor, UIColor.orange.cgColor]

        //定义每种颜色所在的位置
        let gradientLocations:[NSNumber] = gradientLocations ?? [0.0, 1.0]
        
        self.colors = gradientColors
        self.locations = gradientLocations
        
        self.startPoint = startPoint ?? CGPoint(x: 0, y: 0)
        self.endPoint = endPoint ?? CGPoint(x: 1, y: 0)
        
        return self
    }
    
}
